package com.ta.gulimall.order.config.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class AutoRabbitConfig {
    /**
     * 备份交换机
     * @return
     */
    @Bean
    public FanoutExchange backUpExchange(){
        return new FanoutExchange(MqQueueConstant.BACKUP_EXCHANGE_NAME);
    }

    /**
     * 备份队列
     * @return
     */
    @Bean
    public Queue backUpQueue(){
        return QueueBuilder.durable(MqQueueConstant.BACKUP_QUEUE_NAME).build();
    }

    /**
     * 备份交换机与备份队列的绑定
     * @return
     */
    @Bean
    public Binding backUpQueueBingExchange(){
        return BindingBuilder.bind(backUpQueue()).to(backUpExchange());
    }

    @Bean
    public DirectExchange defaultDirectExchange(){
        Map<String, Object> arguments=new HashMap<>(16);
        // 备用交换机
        arguments.put("alternate-exchange",MqQueueConstant.BACKUP_EXCHANGE_NAME);
        return new DirectExchange(MqQueueConstant.DEFAULT_DIRECT_EXCHANGE, true, false, arguments);
    }


//    /**
//     * 警告队列
//     * @return
//     */
//    @Bean("waringQueue")
//    public Queue waringQueue(){
//        return QueueBuilder.durable(MqQueueConstant.WARING_QUEUE_NAME).build();
//    }

//    /**
//     * 备份交换机绑定警告队列
//     * @return
//     */
//    @Bean
//    public Binding waringQueueBingExchange(){
//        return BindingBuilder.bind(waringQueue()).to(backUpExchange());
//    }


    @Bean
    public Queue orderCancelDelayedQuery(){
        return QueueBuilder.durable(MqQueueConstant.ORDER_CANCEL_DELAYED_QUEUE)
                .deadLetterExchange(MqQueueConstant.DEFAULT_DIRECT_EXCHANGE)
                .deadLetterRoutingKey(MqQueueConstant.ORDER_CANCEL_QUEUE)
                .ttl(15 * 60 * 1000)
                .build();
    }

    @Bean
    public Binding orderCancelDelayedBinding(){
        return BindingBuilder.bind(orderCancelDelayedQuery())
                .to(defaultDirectExchange())
                .with(MqQueueConstant.ORDER_CANCEL_DELAYED_QUEUE);
    }

    @Bean
    public Queue orderCancelQuery(){
        return new Queue(MqQueueConstant.ORDER_CANCEL_QUEUE);
    }

    @Bean
    public Binding orderCancelBinding(){
        return BindingBuilder.bind(orderCancelQuery())
                .to(defaultDirectExchange())
                .with(MqQueueConstant.ORDER_CANCEL_QUEUE);
    }

    @Bean
    public Queue storeUnlockDelayedQueue(){
        return QueueBuilder.durable(MqQueueConstant.STORE_UNLOCK_DELAYED_QUEUE)
                .deadLetterExchange(MqQueueConstant.DEFAULT_DIRECT_EXCHANGE)
                .deadLetterRoutingKey(MqQueueConstant.STORE_UNLOCK_QUEUE)
                .ttl(3 * 60 * 1000)
                .build();
    }

    @Bean
    public Binding storeUnlockDelayedBinding(){
        return BindingBuilder.bind(storeUnlockDelayedQueue())
                .to(defaultDirectExchange())
                .with(MqQueueConstant.STORE_UNLOCK_DELAYED_QUEUE);
    }

    @Bean
    public Queue storeUnlockQueue(){
        return new Queue(MqQueueConstant.STORE_UNLOCK_QUEUE);
    }

    @Bean
    public Binding storeUnlockBinding(){
        return BindingBuilder.bind(storeUnlockQueue())
                .to(defaultDirectExchange())
                .with(MqQueueConstant.STORE_UNLOCK_QUEUE);
    }

}
